package JZ_Offer.dataStructure.tree;

import base.TreeNode;

/**
 * 二叉树的镜像
 *
 * 操作给定的二叉树，将其变换为源二叉树的镜像。
 *
 * @Author piper
 * @Date 2019/12/10 2:51 下午
 */
public class JZ27_二叉树的镜像 {

    /**
     * 通过观察 将所有节点的左右节点交换位置即可实现
     */
    private static void Mirror(TreeNode root) {
        if (root == null) {
            return;
        }
        TreeNode left = root.left;
        root.left = root.right;
        root.right = left;
        Mirror(root.left);
        Mirror(root.right);
    }

    public static void main(String[] args) {
        TreeNode a = new TreeNode(1);
        TreeNode b = new TreeNode(2);
        TreeNode c = new TreeNode(3);
        TreeNode d = new TreeNode(4);
        TreeNode e = new TreeNode(5);
        TreeNode f = new TreeNode(6);
        TreeNode g = new TreeNode(7);

        a.left = b;
        a.right = c;
        b.left = d;
        b.right = e;
        c.left = f;
        c.right = g;

        Mirror(a);

        System.out.println(a.val);
        System.out.println(a.left.val + "   " + a.right.val);
        System.out.println(a.left.left.val + "   " + a.left.right.val + "  " + a.right.left.val + "   " + a.right.right.val);

        System.out.println("~~~~~~~~~~~~~~~~~~~~~");

        TreeNode a1 = new TreeNode(8);
        TreeNode a2 = new TreeNode(7);
        TreeNode a3 = new TreeNode(6);
        TreeNode a4 = new TreeNode(5);
        TreeNode a5 = new TreeNode(4);
        a1.left = a2;
        a2.left = a3;
        a3.left = a4;
        a4.left = a5;

        Mirror(a1);

        System.out.println(a1.val);
        System.out.println(a1.right.val);
        System.out.println(a1.right.right.val);
        System.out.println(a1.right.right.right.val);
        System.out.println(a1.right.right.right.right.val);
    }

}
